package fpga_generation.java_base;
import java.io.*;
import java.util.*;
 
public class base_project
{
	public static int IMPL_TARGET_ALTERA_SOPC     = 10;
	public static int IMPL_TARGET_ALTERA_QSYS     = 11;
	public static int IMPL_TARGET_ALTERA_WISHBONE = 12;
	public static int IMPL_TARGET_XILINX_VIVADO   = 20;
	public static int IMPL_TARGET_XILINX_WISHBONE = 22;

	private String itsName;
	private String deviceType;
	private String deviceFamily;
	private int implementation;
	private List<base_component> listOfComponents;
	public static base_clock_source baseClock;
	
	public base_project(String name, int implementation) {
		this.itsName = name;
		this.implementation = implementation;
		this.listOfComponents = new ArrayList<base_component>();
		this.baseClock = null;
	}
	
	public void register(base_component a)
	{
		listOfComponents.add(a);
	}
	
	public void setAsStandardClock(base_clock_source cs) 
	{
		this.baseClock = cs;
	}
	
	public void setDeviceAndFamily(String dev, String family) 
	{
		this.deviceType = dev;
		this.deviceFamily = family;
	}
	
	public void writeResult() 
	{
		if (implementation != IMPL_TARGET_ALTERA_SOPC) {
			System.err.println("Not yet supported...");
			return;
		}
	
		try{
		  // Create file 
		  FileWriter fstream = new FileWriter("export_new.tcl");
		  BufferedWriter out = new BufferedWriter(fstream);
		  
		  out.write("set_system_parameter clockCrossingAdapter \"HANDSHAKE\"\n");
		  out.write("set_system_parameter device \"" + deviceType + "\"\n");
		  out.write("set_system_parameter deviceFamily \"" + deviceFamily + "\"\n");
		  out.write("set_system_parameter deviceSpeedGrade \"\"\n");
		  out.write("set_system_parameter fabricMode \"SOPC\"\n");
		  out.write("set_system_parameter generateLegacySim \"false\"\n");
		  out.write("set_system_parameter generationId \"0\"\n");

		  out.write("set_system_parameter globalResetBus \"true\"\n");
		  out.write("set_system_parameter hdlLanguage \"VHDL\"\n");
		  out.write("set_system_parameter maxAdditionalLatency \"0\"\n");
		  out.write("set_system_parameter projectName \"" + itsName + "\"\n");
		  out.write("set_system_parameter sopcBorderPoints \"true\"\n");
		  out.write("set_system_parameter systemHash \"5715912362\"\n");
		  out.write("set_system_parameter timeStamp \"1369409024020\"\n");
		  out.write("set_system_parameter useTestBenchNamingPattern \"false\"\n");
	     
		  for(int round=0; round<10; round++) {			
			  for(int i=0; i<listOfComponents.size(); i++) {			
				String repr = listOfComponents.get(i).writeRepr(implementation, round);
				if (repr.startsWith("Error")) {
					System.err.println("Couldn't call writeRepr");
				} else {
					out.write(repr);
				}
			  }
		  }
		  
		  //Close the output stream
		  out.close();
		  }catch (Exception e){//Catch exception if any
		  System.err.println("Error: " + e.getMessage());
		}
	}
	public void addComponent(base_component a) 
	{
	
	}
}